namespace Backoffice.Domain.Entities.App;


/// <summary>
/// 系统角色实体
/// </summary>
public class AppRole : EntityBase
{
    public string Name { get; private set; }
    public string? Description { get; private set; }

    // 导航属性
    public virtual List<AppUserRole> UserRoles { get; private set; } = new();
    public virtual List<AppRolePermission> RolePermissions { get; private set; } = new();

    public AppRole(string name, string? description = null)
    {
        Name = name;
        Description = description;
    }

    public void UpdateInfo(string name, string? description)
    {
        Name = name;
        Description = description;
    }

    public void AssignPermission(AppPermission permission)
    {
        if (!RolePermissions.Any(rp => rp.PermissionId == permission.Id))
        {
            RolePermissions.Add(new AppRolePermission(this, permission));
        }
    }

    public void RemovePermission(Guid permissionId)
    {
        var rolePermission = RolePermissions.FirstOrDefault(rp => rp.PermissionId == permissionId);
        if (rolePermission != null)
        {
            RolePermissions.Remove(rolePermission);
        }
    }

}


